# @shuaige : 陈世玉
# @name :toCsv.py
# @time :2024/12/16 20:26
import requests
import csv
from lxml import etree

url = 'https://live.zgzcw.com/ls/AllData.action'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'cookie': 'HWWAFSESID=6efa17c7f94204700c; HWWAFSESTIME=1734070938143; c_rec_sid=26000000000; Hm_lvt_3838657cca9eef8e7b6aa73c17199920=1734070943; Hm_lpvt_3838657cca9eef8e7b6aa73c17199920=1734070943; HMACCOUNT=2F29CE830AA099D9; Hm_lvt_d46894275f280e53aa355be4a6b411d9=1734070943; Hm_lpvt_d46894275f280e53aa355be4a6b411d9=1734070943; qimo_seosource_0=%E5%85%B6%E4%BB%96%E7%BD%91%E7%AB%99; qimo_seokeywords_0=%E6%9C%AA%E7%9F%A5; uuid_174b4710-d317-11eb-9b74-d1a9bcf1ebcf=c43113b2-717d-444a-a72f-edf0a09f2222; qimo_seosource_174b4710-d317-11eb-9b74-d1a9bcf1ebcf=%E5%85%B6%E4%BB%96%E7%BD%91%E7%AB%99; qimo_seokeywords_174b4710-d317-11eb-9b74-d1a9bcf1ebcf=%E6%9C%AA%E7%9F%A5; qimo_xstKeywords_174b4710-d317-11eb-9b74-d1a9bcf1ebcf=; href=https%3A%2F%2Flive.zgzcw.com%2F; accessId=174b4710-d317-11eb-9b74-d1a9bcf1ebcf; pageViewNum=1; JSESSIONID=B113CCA8E3558B9E5319CB1BB95D4B48',
    'referer': 'https://live.zgzcw.com/'
}

fieldnames = ['weekday', 'name', 'round_and_grouping', 'time', 'status', 'zhudui', 'score', 'kedui', 'jinqiu']

with open('data.csv', 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()

    for j in range(1, 13):
        for i in range(1, 31):
            date_str = f'2024-{j:02d}-{i:02d}'
            data = {
                'code': '201',
                'date': '2024-'+str(j)+'-0'+str(i),
                'ajax': 'true'
            }
            r = requests.post(url=url, headers=headers, data=data)
            tree = etree.HTML(r.text)
            weekday = tree.xpath('//td[1]/em/text()')
            name = tree.xpath('//td[2]/span/text()')
            round_and_grouping = tree.xpath('//td[3]/text()')
            time = tree.xpath('//td[4]/text()')
            status = tree.xpath('//td[5]/strong/text()')
            zhudui = tree.xpath('//td[6]/span/a/text()')
            score = tree.xpath('//td[7]/span/text()')
            kedui = tree.xpath('//span[@class="sptl"]/a/text()')
            jinqiu = tree.xpath('//td[@class="f_sel"]/strong[5]/text()')

            for idx in range(len(name)):
                writer.writerow({
                    'weekday': weekday[idx],
                    'name': name[idx],
                    'round_and_grouping': round_and_grouping[idx],
                    'time': time[idx],
                    'status': status[idx],
                    'zhudui': zhudui[idx],
                    'score': score[idx],
                    'kedui': kedui[idx],
                    'jinqiu': jinqiu[idx]
                })
